<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="用户名称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="userList">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="应用ID" align="center" prop="appId" />
      <el-table-column label="OpenID" align="center" prop="openId" :show-overflow-tooltip="true" />
      
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="激活时间" align="center" prop="activateTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.activateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="primary"
            link
            icon="View"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            type="primary"
            link
            icon="ChatDotRound"
            @click="handleMessage(scope.row)"
          >对话记录</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 用户详情对话框 -->
    <el-dialog title="用户详情" v-model="dialog.visible" width="500px" append-to-body>
      <el-descriptions :column="1" border>
        <el-descriptions-item label="用户ID">{{ form.id }}</el-descriptions-item>
        <el-descriptions-item label="应用ID">{{ form.appId }}</el-descriptions-item>
        <el-descriptions-item label="OpenID">{{ form.openId }}</el-descriptions-item>
        
        <el-descriptions-item label="创建时间">{{ parseTime(form.createTime) }}</el-descriptions-item>
        <el-descriptions-item label="激活时间">{{ parseTime(form.activateTime) }}</el-descriptions-item>
        <el-descriptions-item label="用户资料">
          <div style="white-space: pre-wrap;">{{ form.userProfile }}</div>
        </el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialog.visible = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="AppCustomer">
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { listCustomer } from "@/api/app-store/customer";

// 遮罩层
const loading = ref(false);
// 总条数
const total = ref(0);
// 用户表格数据
const userList = ref([]);

// 查询参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  nickName: undefined
});

// 弹出层标题
const dialog = ref({
  visible: false
});

// 表单参数
const form = ref({});

/** 查询用户列表 */
function getList() {
  loading.value = true;
  listCustomer(queryParams.value).then(response => {
    userList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value.nickName = undefined;
  handleQuery();
}

/** 查看详情按钮操作 */
function handleDetail(row) {
  form.value = { ...row };
  dialog.value.visible = true;
}

/** 查看对话记录按钮操作 */
const router = useRouter();
function handleMessage(row) {
  router.push({
    path: '/message',
    query: { id: row.id }
  });
}

/** 初始化数据 */
getList();
</script>